package cn.getech.data.metadata.service.impl;

import cn.getech.data.metadata.controller.module.ModuleUtils;
import cn.getech.data.metadata.entity.CollectionSystemModuleEntity;
import cn.getech.data.metadata.entity.meta.CollectionTableInfo;
import cn.getech.data.metadata.model.res.CollectionTableOperateInner;
import cn.getech.data.metadata.model.res.CollectionTableOperateType2Res;
import cn.getech.data.metadata.service.CollectionSystemModuleService;
import cn.getech.data.metadata.service.CollectionTableInfoService;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.service.SysUserService;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.intelligence.common.utils.PageUtils2;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.data.intelligence.common.exception.RRException;

import cn.getech.data.metadata.mapper.CollectionTableOperateMapper;
import cn.getech.data.metadata.entity.CollectionTableOperateEntity;
import cn.getech.data.metadata.service.CollectionTableOperateService;
import cn.getech.data.metadata.model.req.collectiontableoperate.*;
import cn.getech.data.metadata.model.res.CollectionTableOperateListRes;


@Service("collectionTableOperateService")
public class CollectionTableOperateServiceImpl extends ServiceImpl<CollectionTableOperateMapper, CollectionTableOperateEntity> implements CollectionTableOperateService {
    @Autowired
    private CollectionSystemModuleService collectionSystemModuleService;
    @Autowired
    private CollectionTableInfoService collectionTableInfoService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils2<CollectionTableOperateEntity> queryPage(Map<String, Object> params) {
        IPage<CollectionTableOperateEntity> page = this.page(
                new Query<CollectionTableOperateEntity>().getPage(params),
                new QueryWrapper<CollectionTableOperateEntity>()
        );

        return new PageUtils2<CollectionTableOperateEntity>(page);
    }

    @Override
    public PageUtils2<CollectionTableOperateListRes> list(CollectionTableOperateListReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("tableId", req.getTableId());
        if (req.getLimit() != null && req.getPage() != null) {
            params.put("offset", (req.getPage() - 1) * req.getLimit());
            params.put("limit", req.getLimit());
        }
        List<CollectionTableOperateListRes> list = baseMapper.listCollectionTableOperate(params);
        Integer count = baseMapper.listCollectionTableOperateCount(params);
        PageUtils2<CollectionTableOperateListRes> pageUtils = new PageUtils2(list, count, req.getLimit(), req.getPage());
        return pageUtils;
    }

    @Override
    public CollectionTableOperateType2Res info(Long operateId) {
        CollectionTableOperateEntity operateEntity = baseMapper.selectById(operateId);
        if(operateEntity == null)
            throw new RRException("没有该记录");
        if (Integer.valueOf(2).equals(operateEntity.getOperateType())) {
            CollectionTableOperateInner obj = JSONUtil.toBean(operateEntity.getOperateType2Res(), CollectionTableOperateInner.class);
            List<CollectionSystemModuleEntity> moduleList = collectionSystemModuleService.list();
            String oldModulName = "";
            if (obj.getOldOwnModule() != null) {
                oldModulName = ModuleUtils.getAllPath(moduleList, obj.getOldOwnModule());
            }
            String newModulName = "";
            if (obj.getNewOwnModule() != null) {
                newModulName = ModuleUtils.getAllPath(moduleList, obj.getNewOwnModule());
            }
            CollectionTableInfo tableInfo = collectionTableInfoService.getById(operateEntity.getTableId());
            SysUserEntity userEntity = sysUserService.getById(operateEntity.getUserId());
            CollectionTableOperateType2Res res = new CollectionTableOperateType2Res();
            res.setOldModule(oldModulName);
            res.setNewModule(newModulName);
            res.setTableName(tableInfo.getTableName());
            res.setUsername(userEntity.getUsername());
            res.setOperateTime(operateEntity.getOperateTime());
            return res;
        }

        return null;
    }

    @Override
    public void saveTableOperate(List<CollectionTableOperateEntity> tableOperateList) {
        if (tableOperateList.size() > 0) {
            tableOperateList = tableOperateList.stream().distinct().collect(Collectors.toList());
            int start = 0, limit = 200;
            List<CollectionTableOperateEntity> collect = tableOperateList.stream().skip(start).limit(limit).collect(Collectors.toList());
            while (collect != null && collect.size() > 0) {
                saveBatch(collect);

                start += limit;
                collect = tableOperateList.stream().skip(start).limit(limit).collect(Collectors.toList());
            }
        }
    }
}
